package com.smbms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smbms.mapper.UserMapper;
import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.util.IdWorker;
import com.smbms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private IdWorker idWorker;

    @Override
    public User login(String userCode, String userpwd) throws Exception {
        //根据用户编号查找用户
        User user = userMapper.selectUserByUserCode(userCode);
        if(user==null){
            throw  new Exception("用户不存在");
        }
        //判断密码是否相同
        if(!bCryptPasswordEncoder.matches(userpwd,user.getUserpassword())){
            throw  new Exception("密码不正确");
        }
        return user;

    }

    /**
     * readOnly: true，查询时设置为true可提高性能
     * timeout: 事务超时时间，-1表示无限制
     * @return
     * @throws Exception
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class,timeout = -1)
    @Override
    public PageResult<User> showUserListByPage(Integer pageNo, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNo,pageSize);
        List<User> userList = userMapper.selectAll();
        PageInfo pageInfo=new PageInfo(userList);
        PageResult<User> pageResult=new PageResult<>(pageInfo.getTotal(),userList);
        return pageResult;
    }

    /**
     * propagation:设置事务的传播行为
     * isolation：设置事务的隔离级别，通常默认为数据库的默认级别
     * rollbackFor： 回滚事务
     * @param user
     * @return
     * @throws Exception
     */
    @Transactional(propagation= Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public boolean addUser(User user) throws Exception {
        //先对明文密码进行加密
        String newPwd = bCryptPasswordEncoder.encode(user.getUserpassword());
        user.setUserpassword(newPwd);
        System.out.println("加密后的pwd:"+newPwd);
        //设置id
        user.setId(idWorker.nextId());//采用分步式自增长
        int row = userMapper.insertUser(user);
        if(row!=1){
            //插入数据失败，回滚事务
            throw new Exception("增加用户失败");
        }
        return true;
    }


    @Override
    public List<User> showUserListByName(String userName) throws Exception {
        return userMapper.selectUserListByName(userName);
    }
     @Transactional(readOnly = true)
    @Override
    public User showUserInfoById(Long id) throws Exception {
        return userMapper.selectUserById(id);
    }

    @Transactional
    @Override
    public boolean del(Long id) throws Exception {
        int row=userMapper.deleteById(id);
        if(row!=1)
            throw new Exception("删除失败");
        return true;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public boolean updateUser(User user) throws Exception {
        if(userMapper.updateUserById(user)!=1){
            //修改失败
            throw new Exception("修改失败");
        }
        //以后下面有很多业务逻辑
        return true;
    }
}
